package cn.tb.system.service.impl;

import cn.tb.system.domain.Role;
import cn.tb.system.mapper.RoleMapper;
import cn.tb.system.service.IRoleService;
import cn.tb.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tangbo
 * @since 2022-06-22
 */
@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void save(Role role) {
        //1.保存role表的数据
        roleMapper.save(role);
        //2.保存中间表
        initRelationTable(role);
    }

    private void initRelationTable(Role role) {
        //前端要求必须传递最新的全量数据， 直接做全量覆盖
        //1.删除历史数据
        roleMapper.removeRoleMenuByRoleId(role.getId());
        roleMapper.removeRolePermissionByRoleId(role.getId());
        //2.保存角色菜单表数据
        roleMapper.saveRoleMenu(role);
        //3.保存角色权限表数据
        roleMapper.saveRolePermission(role);

    }

    @Override
    @Transactional
    public void update(Role role) {
        this.update(role);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        this.deleteById(id);
    }
}
